{% extends 'base.html' %}

{% block title %}种植数据列表{% endblock %}

{% block content %}
<div class="card">
    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
        <h4 class="mb-0">种植数据管理</h4>
        <a href="{{ url_for('data.planting_add') }}" class="btn btn-light">添加种植数据</a>
    </div>
    <div class="card-body">
        <!-- 搜索表单 -->
        <form method="get" class="mb-4">
            <div class="row g-3">
                <div class="col-md-3">
                    <input type="text" class="form-control" name="search_crop" value="{{ search_crop }}" placeholder="搜索作物类型...">
                </div>
                <div class="col-md-3">
                    <input type="text" class="form-control" name="search_region" value="{{ search_region }}" placeholder="搜索种植区域...">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_from" value="{{ date_from }}" placeholder="开始日期">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_to" value="{{ date_to }}" placeholder="结束日期">
                </div>
                <div class="col-md-1">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
                <div class="col-md-1">
                    <a href="{{ url_for('data.planting_list') }}" class="btn btn-secondary w-100">重置</a>
                </div>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>作物类型</th>
                        <th>面积(亩)</th>
                        <th>种植区域</th>
                        <th>地块编号</th>
                        <th>种植日期</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in pagination.items %}
                    <tr>
                        <td>{{ item.crop_type }}</td>
                        <td>{{ item.area }}</td>
                        <td>{{ item.region }}</td>
                        <td>{{ item.plot_number }}</td>
                        <td>{{ item.planting_date.strftime('%Y-%m-%d') }}</td>
                        <td>{{ item.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ url_for('data.planting_edit', id=item.id) }}" class="btn btn-primary">编辑</a>
                                <form action="{{ url_for('data.planting_delete', id=item.id) }}" method="post" class="d-inline" onsubmit="return confirm('确定要删除这条记录吗？');">
                                    <button type="submit" class="btn btn-danger">删除</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        <!-- 更新分页，包含搜索参数 -->
        {% if pagination.pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                    <a class="page-link" href="{% if pagination.has_prev %}{{ url_for('data.planting_list', page=pagination.prev_num, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        上一页
                    </a>
                </li>
                
                {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                    {% if page %}
                        <li class="page-item {% if page == pagination.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('data.planting_list', page=page, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}">
                                {{ page }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">...</span>
                        </li>
                    {% endif %}
                {% endfor %}
                
                <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                    <a class="page-link" href="{% if pagination.has_next %}{{ url_for('data.planting_list', page=pagination.next_num, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        下一页
                    </a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>
{% endblock %} 